package security.Authentication.Base;

/**
 * Interface for authentication handler by using the pattern chain of
 * responsibility
 * @author Andreas Carmvall
 */
public abstract class AuthenticationHandler {

	/**
	 * The next handler in the chain
	 */
	protected AuthenticationHandler next;
	
	/**
	 * Pass the request to the next handler in the chain
	 * @param request the authentication request
	 * @return the authenticated user, either an identified or unknown user
	 */
	protected User passRequestToNext(AuthenticationRequest request) {
		if (next != null) {
			return next.handleRequest(request);
		} else {
			return new UnknownUser("No handlers handled the request", false);
		}
	}

	/**
	 * Set reference to the next handler in the chain
	 * @param handler the handler set as next in the chain
	 */
	public void setNext(AuthenticationHandler handler) {
		next = handler;
	}
	
	/**
	 * Handle an authentication request if the request fits this handler
	 * otherwise pass it to the next handler in the chain
	 * @param request the authentication request
	 * @return the authenticated user, either an identified or unknown user
	 */
	public abstract User handleRequest(AuthenticationRequest request);
}
